﻿using JNPF.Basics.Models;
using JNPF.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 公众号用户
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-09-12 
    /// </summary>
    public class MPUserBll
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<MPUserModel>> GetList(PageInput input)
        {
            try
            {
                var output = new PageResult<MPUserModel>();
                output.pagination = input.MapTo<PageOutput>();
                var config = await new SysConfigBll().GetInfo("MPConfig");
                var resultJObject = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).GetUserList().ToObject();
                var openIdList = JsonHelper.ToList<string>(resultJObject["openid"].ToString());
                if (openIdList != null)
                {
                    var usersJson = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchGetUserInfo(openIdList);
                    output.pagination.total = resultJObject["total"].ToInt();
                    output.list = JsonHelper.ToList<MPUserModel>(usersJson);
                }
                return output;
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<MPUserModel>> GetList()
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                var resultJObject = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).GetUserList().ToObject();
                var openIdList = JsonHelper.ToList<string>(resultJObject["openid"].ToString());
                if (openIdList != null)
                {
                    var usersJson = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchGetUserInfo(openIdList);
                    return JsonHelper.ToList<MPUserModel>(usersJson);
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 获取标签下粉丝列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <param name="tagId">标签Id</param>
        /// <returns></returns>
        public async Task<PageResult<MPUserModel>> GetListByTagId(PageInput input, string tagId)
        {
            try
            {
                var output = new PageResult<MPUserModel>();
                output.pagination = input.MapTo<PageOutput>();
                var config = await new SysConfigBll().GetInfo("MPConfig");
                string nextOpenid = "";
                var resultJObject = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).GetTagUsers(tagId.ToInt(), nextOpenid).ToObject();
                var openIdList = JsonHelper.ToList<string>(resultJObject["openid"].ToString());
                if (openIdList != null)
                {
                    var usersJson = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchGetUserInfo(openIdList);
                    output.pagination.total = resultJObject["total"].ToInt();
                    output.list = JsonHelper.ToList<MPUserModel>(usersJson);
                }
                return output;
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 黑名单列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<MPUserModel>> GetBlackList(PageInput input)
        {
            try
            {
                var output = new PageResult<MPUserModel>();
                output.pagination = input.MapTo<PageOutput>();
                var config = await new SysConfigBll().GetInfo("MPConfig");
                var resultJObject = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).GetBlackList().ToObject();
                var openIdList = JsonHelper.ToList<string>(resultJObject["openid"].ToString());
                if (openIdList != null)
                {
                    var usersBlackJson = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchGetUserInfo(openIdList);
                    output.pagination.total = resultJObject["total"].ToInt();
                    output.list = JsonHelper.ToList<MPUserModel>(usersBlackJson); ;
                }
                return output;
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 用户信息
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task<MPUserModel> UserInfo(string openId)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                return new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).UserInfo(openId);
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 修改关注者备注信息
        /// </summary>
        /// <param name="userModel">用户模型类</param>
        /// <returns></returns>
        public async Task UpdateRemark(MPUserModel userModel)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).UpdateRemark(userModel.openid, userModel.remark);
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 加入黑名单
        /// </summary>
        /// <param name="openId">关注者Id</param>
        public async Task AddBatchBlack(string openId)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                List<string> openIdList = new List<string>(openId.Split(','));
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchBlackList(openIdList);
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 移除黑名单
        /// </summary>
        /// <param name="openId">关注者Id</param>
        public async Task DeleteBatchUnBlack(string openId)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                List<string> openIdList = new List<string>(openId.Split(','));
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).BatchUnBlackList(openIdList);
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger().Error(ex);
                throw ExceptionEx.Throw(ex);
            }
        }
    }
}
